package com.crealth.handler;

import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crealth.ConstantDefine;
import com.crealth.crmserver.CRMServerHandler;
import com.crealth.db.KnowledgeCreditLoanDb;
import com.crealth.db.KnowledgePledgetLoanDb;
import com.crealth.memcached.UserCached;
import com.crealth.model.CompanyModel;
import com.crealth.model.KnowledgeCreditLoanModel;
import com.crealth.model.KnowledgePledgetLoanModel;
import com.crealth.model.UserModel;
import com.crealth.msg.NetMessageData;

public class ProductKnowledgeHandler {

	public static NetMessageData obtainAllKnowledge(NetMessageData revData) {
		NetMessageData sendMsg = null;
		JSONObject resJson = null;
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		CompanyModel belCom = user.getBelongCompany();
		String comCityName = "";
		if (belCom != null) {
			comCityName = belCom.getCityName();
		}
		boolean needCityName = false;
		int uPos = user.getPosition();
		if (uPos >= ConstantDefine.Head_Manager || 
			uPos == ConstantDefine.Installmenter || 
			uPos == ConstantDefine.InstallDpmMGR) {
			needCityName = true;
		}
		
		List<KnowledgeCreditLoanModel> creditKnowledgels = null;
		List<KnowledgePledgetLoanModel> pledgetKnowledgels = null;
		if (!needCityName && comCityName.length() > 0) {			
			creditKnowledgels = KnowledgeCreditLoanDb.selectCityNameKnowledgeCreditLoan(comCityName);
			pledgetKnowledgels = KnowledgePledgetLoanDb.selectCityNameKnowledgePledgetLoan(comCityName);
		} else {
			creditKnowledgels = KnowledgeCreditLoanDb.selectAllKnowledgeCreditLoan();
			pledgetKnowledgels = KnowledgePledgetLoanDb.selectAllKnowledgePledgetLoan();
		}
		int creditKnowledgeCnt = creditKnowledgels.size();
		int pledgetKnowledgeCnt = pledgetKnowledgels.size();
		int bagNum = 5;
		if (creditKnowledgels != null && creditKnowledgels.size() > 0) {
			JSONArray creditArray = new JSONArray();			
			for (int idx = 0; idx < creditKnowledgeCnt; ++idx) {
				KnowledgeCreditLoanModel creditKnowledge = creditKnowledgels.get(idx);
				creditArray.add(creditKnowledge.toJSONObject());
				if (creditArray.size() == bagNum || idx == creditKnowledgeCnt-1) {
					resJson = new JSONObject();
					resJson.put("resCode", 0);
					resJson.put("creditKnowledgeArr", creditArray);
					resJson.put("creditKnowledgeCnt", creditKnowledgeCnt);
					resJson.put("pledgetKnowledgeCnt", pledgetKnowledgeCnt);
					sendMsg = new NetMessageData();
					sendMsg.setMsgBody(resJson.toJSONString());
					ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), userID, sendMsg);
				}
				if (creditArray.size() == bagNum) {
					creditArray = new JSONArray();
				}
			}
		}		
		
		if (pledgetKnowledgels != null && pledgetKnowledgels.size() > 0) {
			JSONArray pledgetArray = new JSONArray();			
			for (int idx = 0; idx < pledgetKnowledgeCnt; ++idx) {
				KnowledgePledgetLoanModel pledgetKnowledge = pledgetKnowledgels.get(idx);
				pledgetArray.add(pledgetKnowledge.toJSONObject());
				if (pledgetArray.size() == bagNum || idx == pledgetKnowledgeCnt-1) {
					resJson = new JSONObject();
					resJson.put("resCode", 0);
					resJson.put("pledgetKnowledgeArr", pledgetArray);
					resJson.put("creditKnowledgeCnt", creditKnowledgeCnt);
					resJson.put("pledgetKnowledgeCnt", pledgetKnowledgeCnt);
					sendMsg = new NetMessageData();
					sendMsg.setMsgBody(resJson.toJSONString());
					ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), userID, sendMsg);
				}
				if (pledgetArray.size() == bagNum) {
					pledgetArray = new JSONArray();
				}
			}
		}
		
		return null;
	}
	
	public static NetMessageData savePledgetKnowledge(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() < ConstantDefine.Head_Manager 
				&& user.getPosition() != ConstantDefine.InstallDpmMGR 
				&& user.getPosition() != ConstantDefine.Installmenter ) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		String strKnowledge = msgJson.getString("pledgetKnowledge");
		JSONObject pledgetJson = JSONObject.parseObject(strKnowledge);
		if (pledgetJson == null) {
			return null;
		}
		KnowledgePledgetLoanModel pledgetKnowledge = new KnowledgePledgetLoanModel();
		pledgetKnowledge.parseFromJson(pledgetJson);
		String klgName = pledgetKnowledge.getName();
		if (klgName == null || klgName.length() == 0) {
			resJson.put("resCode", ErrorCode.ERROR_KLG_NAME_NULL);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		KnowledgePledgetLoanModel existKlg = KnowledgePledgetLoanDb.selectKnowledgePledgetLoanByName(klgName);
		if (existKlg != null) {
			pledgetKnowledge.setId(existKlg.getId());
		}		
		if (pledgetKnowledge.getId() > 0) {
			KnowledgePledgetLoanDb.updateKnowledgePledgetLoanByID(pledgetKnowledge);
		}
		else {
			KnowledgePledgetLoanDb.insertKnowledgePledgetLoan(pledgetKnowledge);
			pledgetJson = pledgetKnowledge.toJSONObject();
		}
		
		resJson.put("pledgetKnowledge", pledgetJson);	
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		sendMsg.setMsgCmd(revData.getMsgCmd());
		sendMsg.setMsgBody(resJson.toJSONString());

		String cityName = pledgetKnowledge.getCityName();
		if (cityName != null && cityName.length() > 0) {
			CRMServerHandler.boradMsgToCompany(userID, cityName, sendMsg);
			ResponseMsgQueue.sendMsgToMortgager(userID, user.getBelongCompanyID(), sendMsg);
			ResponseMsgQueue.sendMsgToMortgagerMgr(userID, user.getBelongCompanyID(), sendMsg);
			ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
			
			return sendMsg;
		} else {
			CRMServerHandler.sendMsgToClients(sendMsg);
			return null;
		}	
		
	}
	
	public static NetMessageData saveCreditKnowledge(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() < ConstantDefine.Head_Manager
				&& user.getPosition() != ConstantDefine.InstallDpmMGR 
				&& user.getPosition() != ConstantDefine.Installmenter ) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		String strKnowledge = msgJson.getString("creditKnowledge");
		JSONObject creditJson = JSONObject.parseObject(strKnowledge);
		if (creditJson == null) {
			return null;
		}
		
		KnowledgeCreditLoanModel creditKnowledge = new KnowledgeCreditLoanModel();
		creditKnowledge.parseFromJson(creditJson);
		String klgName = creditKnowledge.getName();
		if (klgName == null || klgName.length() == 0) {
			resJson.put("resCode", ErrorCode.ERROR_KLG_NAME_NULL);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		KnowledgeCreditLoanModel existKlg = KnowledgeCreditLoanDb.selectKnowledgeCreditLoanByName(klgName);
		if (existKlg != null) {
			creditKnowledge.setId(existKlg.getId());
		}	
		if (creditKnowledge.getId() > 0) {
			KnowledgeCreditLoanDb.updateKnowledgeCreditLoanByID(creditKnowledge);
		}
		else {
			KnowledgeCreditLoanDb.insertKnowledgeCreditLoan(creditKnowledge);			
		}
		creditJson = creditKnowledge.toJSONObject();
		resJson.put("creditKnowledge", creditJson);
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		sendMsg.setMsgCmd(revData.getMsgCmd());
		sendMsg.setMsgBody(resJson.toJSONString());
		
		String cityName = creditKnowledge.getCityName();
		if (cityName != null && cityName.length() > 0) {
			CRMServerHandler.boradMsgToCompany(userID, cityName, sendMsg);
			ResponseMsgQueue.sendMsgToMortgager(userID, user.getBelongCompanyID(), sendMsg);
			ResponseMsgQueue.sendMsgToMortgagerMgr(userID, user.getBelongCompanyID(), sendMsg);
			ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
			
			return sendMsg;
		} else {
			CRMServerHandler.sendMsgToClients(sendMsg);
			return null;
		}
		
	}
	
	public static NetMessageData deleteKnowledge(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() < ConstantDefine.Head_Manager) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		int klgType = msgJson.getIntValue("klgType");
		int klgID = msgJson.getIntValue("klgID");
		Integer res = null;
		if (klgType == 1) {
			res = KnowledgeCreditLoanDb.deleteCreditKnowledgeByID(klgID);
		} else if (klgType == 2) {
			res = KnowledgePledgetLoanDb.deletePledgetKnowledgeByID(klgID);
		}
		
		if (res != null && res > 0) {
			resJson.put("klgType", klgType);
			resJson.put("klgID", klgID);
		}
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		sendMsg.setMsgCmd(revData.getMsgCmd());
		sendMsg.setMsgBody(resJson.toJSONString());
		CRMServerHandler.sendMsgToClients(sendMsg);
		
		return null;
	}
	
}
